package cn.iocoder.yudao.module.digital.api.cardkeymanager;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.digital.api.cardkeymanager.dto.CardKeySecretDTO;
import cn.iocoder.yudao.module.digital.dal.dataobject.cardkeysecret.CardkeySecretDO;
import cn.iocoder.yudao.module.digital.service.cardchannel.ICardSecretManager;
import cn.iocoder.yudao.module.digital.service.cardkeysecret.CardkeySecretService;

import java.util.Collections;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: 卡密管理
 * Author: li
 * Date: 2024/3/1
 */
@Service
public class CardKeyManagerApiImpl implements ICardKeyManagerApi {
    @Resource
    private ICardSecretManager cardKeyManagerService;

    @Resource
    private CardkeySecretService cardkeySecretService;

    @Override
    public List<CardKeySecretDTO> claimCardKeySecretByCustomerService(String orderNo, String secret) {
        return cardKeyManagerService.claimCardKeySecretByCustomerService(orderNo, secret);
    }

    @Override
    public void claimCardKeySecret(String orderNo, Long skuId, int skuNum, Integer buyDenomination) {
        cardKeyManagerService.preprocessCardKeyShipping(orderNo, skuId, skuNum, buyDenomination);
    }

    @Override
    public List<CardKeySecretDTO> getCardkeySecretByOrderNo(Long userId, String orderNo) {
        List<CardkeySecretDO> secretDOList = cardkeySecretService.getCardKeySecretByOrderNo(userId, orderNo);
        return secretDOList.stream().map(v -> new CardKeySecretDTO(v.getCardNumber(), v.getCode(), v.getPin())).collect(Collectors.toList());
    }

    @Override
    public List<String> getOrderNoByCardKeySecret(String cardKeySecret) {
        List<CardkeySecretDO> secretDOList = cardkeySecretService.getByCardKeySecret(cardKeySecret);
        if (CollectionUtil.isNotEmpty(secretDOList)) {
            return secretDOList.stream().map(CardkeySecretDO::getOrderNo).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

}
